/*
 * @Author: caofujun
 * @Date: 2020-07-23 11:01:41
 * @Description:  创建页面命令
 */

const nameExist = require('../utils/index.js')

module.exports = {
  description: '创建page页面',
  prompts: [
    {
      type: 'input',
      name: 'dir',
      message: '请输入 page 文件夹名称!',
      validate: value => {
        if ((/.+/).test(value)) {
          return nameExist(value) ? '组件名 或 page名已经存在' : true
        }
        return '请输入page 文件夹名称'
      }
    },
    {
      type: 'list',
      name: 'pageType',
      suffix: 'base(基础页面)，crud(Crud页面)',
      choices: [
        'base',
        'crud'
      ]
    },
    {
      type: 'list',
      name: 'menuType',
      message: '新建页面菜单级别',
      suffix: 'menu1(一级菜单)，menu2(二级菜单)',
      choices: [
        'menu1',
        'menu2'
      ]
    },
    {
      type: 'input',
      name: 'menu1Dir',
      message: '请输入一级菜单文件夹名称!',
      when: data => {
        console.log(data)
        return data.menuType === 'menu2'
      },
      validate: value => {
        if (!(/.+/).test(value)) {
          return '请输入一级菜单文件夹名称'
        }
        return true
      }
    },
    {
      type: 'confirm',
      name: 'hasApi',
      default: true,
      message: '是否创建api文件'
    },
    {
      type: 'confirm',
      name: 'hasRouter',
      default: true,
      message: '是否添加路由'
    }
    // {
    //   type: 'confirm',
    //   name: 'hasComponents',
    //   default: true,
    //   message: '是否创建components文件夹?'
    // }
  ],
  actions: (data) => {
    const { hasApi, pageType, menuType, hasRouter } = data
    const actions = []
    if (menuType === 'menu2') {
      actions.push({
        type: 'add',
        path: './src/views/business/{{menu1Dir}}/{{camelCase dir}}/index.vue',
        templateFile: `generator/page/page.${pageType}.hbs`
      })
    } else {
      actions.push({
        type: 'add',
        path: './src/views/business/{{camelCase dir}}/index.vue',
        templateFile: `generator/page/page.${pageType}.hbs`
      })
    }

    if (hasApi) {
      actions.push({
        type: 'add',
        path: './src/api/business/{{camelCase dir}}.js',
        templateFile: 'generator/page/request.hbs'
      })
    }

    if (hasRouter) {
      actions.push({
        type: 'append',
        pattern: /\[/,
        path: './src/router/routers.js',
        templateFile: 'generator/page/router.hbs'
      })
    }

    // if (hasComponents) {
    //   actions.push({
    //     type: 'add',
    //     path: './src/views/business/{{camelCase dir}}/components/.gitkeep'
    //   })
    // }

    return actions
  }
}
